package com.dream.service;

import com.dream.entity.JwtUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Collections;

/**
 * 查询数据库，验证用户
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService{

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询数据库，返回查询后的的对象

        //验证对象是否存在，不存在抛出异常：throw new UsernameNotFoundException(username);

        //校验成功后 返回JWT验证对象，密码应该是加密存在数据库里的
        String password = new BCryptPasswordEncoder().encode("admin");
        return new JwtUser("admin",password, Collections.emptyList());
    }
}
